package de.reichelt.fxrequi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

public class Config {

  private static String PROP_FILE = "config.properties";
  
  private Properties props = null;
  public static Config cfg = null;
  
  public static Config getInstance() throws Exception {
    if (cfg==null) {
      cfg = new Config();
    }
    return cfg;
  }
  
  public static void setPropertyFile(String filespec) {
    PROP_FILE = filespec;
  }
  
  private Config() throws Exception {
    String folder = FXrequi.PRODUCTION?System.getProperty("user.home"):"./";
    File fo = new File(folder);
    if (!fo.exists()) {
      fo.mkdir();
    }
    File f = new File(PROP_FILE);
    props = new Properties();
    if (!f.exists()) {
      FileOutputStream fos = new FileOutputStream(f);
      props.put("db.driver", "org.apache.derby.jdbc.EmbeddedDriver");
      props.put("db.protocol", "jdbc:derby:");
      props.put("db.name", "requi.db");
      props.put("db.user", "fxrequi");
      props.put("db.passwd", "fxrequi");
      props.put("loglevel", "trace");
      props.store(fos, "Konfiguration");
      fos.close();
      props = new Properties();
    }
    //now read the file contents
    InputStream is = null;
    try {
      is = new FileInputStream(f);
      props.load(is);
    } finally {
      if (is!=null) {is.close();}
    }
  }
  
  public String getString(String key) throws Exception {
    if (!props.containsKey(key)) {
      throw new Exception("Konfiguration fuer '"+key+"' nicht gefunden!");
    }
    return props.getProperty(key);
  }
  
  public Integer getInt(String key) throws Exception {
    if (!props.containsKey(key)) {
      throw new Exception("Konfiguration fuer '"+key+"' nicht gefunden!");
    }
    return Integer.parseInt(props.getProperty(key));
  }
  
  public Double getDouble(String key) throws Exception {
    if (!props.containsKey(key)) {
      throw new Exception("Konfiguration fuer '"+key+"' nicht gefunden!");
    }
    return Double.parseDouble(props.getProperty(key));
  }
  
  public Boolean getBoolean(String key) throws Exception {
    if (!props.containsKey(key)) {
      throw new Exception("Konfiguration fuer '"+key+"' nicht gefunden!");
    }
    return Boolean.parseBoolean(props.getProperty(key));
  }
  
  public void setString(String key, String value) {
    props.put(key, value);
  }
  
  public void setInt(String key, Integer value) {
    props.put(key, value.toString());
  }
  
  public void setDouble(String key, Double value) {
    props.put(key, value.toString());
  }
  
  public void setBoolean(String key, Boolean value) {
    props.put(key, value.toString());
  }
  
  public boolean hasKey(String key) {
    return props.containsKey(key) && !((String)props.get(key)).isEmpty();
  }
  
  public void store() throws Exception {
    FileOutputStream fos = new FileOutputStream(PROP_FILE);
    props.store(fos, "aktuelle Werte");
    fos.flush();
    fos.close();
  }

}
